package com.myjava.managesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myjava.managesystem.common.R;
import com.myjava.managesystem.entity.Category;
import com.myjava.managesystem.entity.Employee;
import com.myjava.managesystem.service.CategoryService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @GetMapping("/page")
    public R<IPage> getPage(@RequestParam Integer page, @RequestParam Integer pageSize){
        Page<Category> p = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        IPage<Category> iPage = categoryService.page(p,wrapper);
        return R.success(iPage);
    }

    @PostMapping
    public R<String> insert(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }

    @DeleteMapping
    public R<String> deleteCategory(@RequestParam Long ids){
        categoryService.removeCategory(ids);
        return R.success("添加成功");
    }

    @PutMapping
    public R<String> update(@RequestBody(required = false) Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType()!=null,Category::getType,category.getType());
        wrapper.orderByAsc(Category::getSort);
        return R.success(categoryService.list(wrapper));
    }
}
